package com.dragon.logical.service.contract.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.util.JwtUtil;
import com.dragon.logical.service.contract.ICommentService;
import com.dragon.repository.entity.Comment;
import com.dragon.repository.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-24
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    CommentMapper commentMapper;

    /**
     * 存放迭代找出的所有子代的集合
     */
    private List<Comment> tempReplies = new ArrayList<>();

    @Override
    public List<Comment> listComment(Integer correlationId, Integer streamType) {
        //查询出父节点
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("M.parent_id", 0)
                .eq(correlationId != null, "M.correlation_id", correlationId)
                .eq(streamType != null, "M.stream_type", streamType)
                .orderByDesc("M.create_time");
        List<Comment> comments = commentMapper.findByParentIdNull(wrapper);
        for(Comment comment : comments){
            List<Comment> childComments = commentMapper.findByParentIdNotNull(comment.getId());
            //查询出子评论
            combineChildren(childComments);
            comment.setSubComments(tempReplies);
            tempReplies = new ArrayList<>();
        }
        return comments;
    }

    /**
     * @Description: 查询出子评论
     * @Param: childComments：所有子评论
     * @Param: parentNickname1：父评论的姓名
     * @Return:
     */
    private void combineChildren(List<Comment> childComments) {
        //判断是否有一级子回复
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(Comment childComment : childComments){
                tempReplies.add(childComment);
                //查询二级以及所有子集回复
                recursively(childComment.getId());
            }
        }
    }

    /**
     * @Description: 循环迭代找出子集回复
     * @Param: childId：子评论的id
     * @Param: parentNickname1：子评论的姓名
     * @Return:
     */
    private void recursively(Integer childId) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replayComments = commentMapper.findByReplayId(childId);

        if(replayComments.size() > 0){
            for(Comment replayComment : replayComments){
                tempReplies.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayComment.getId());
            }
        }
    }

    @Override
    public void addComment(Comment comment, HttpServletRequest request) {
        String adminId = JwtUtil.getAdminId(request);
        comment.setCreateTime(DateUtil.date());
        comment.setAdminId(Integer.valueOf(adminId));
        commentMapper.insert(comment);
    }

    @Override
    public void replyComment(Comment comment, HttpServletRequest request) {
        //获取被回复的评论
        Comment replyComment = commentMapper.selectById(comment.getReplyId());
        comment.setAdminId(Integer.valueOf(JwtUtil.getAdminId(request)));
        comment.setParentId(replyComment.getId());
        comment.setCreateTime(DateUtil.date());
        comment.setReplyAdminId(replyComment.getAdminId());
        comment.setCorrelationId(replyComment.getCorrelationId());
        comment.setStreamType(replyComment.getStreamType());
        commentMapper.insert(comment);
    }

    @Override
    public void deleteComment(Integer id, Integer streamType, HttpServletRequest request){
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        Integer adminId = Integer.valueOf(JwtUtil.getAdminId(request));
        wrapper.lambda()
                .eq(Comment::getId,id)
                .eq(Comment::getStreamType, streamType)
                .eq(Comment::getAdminId,adminId);
        List<Comment> comments = commentMapper.selectList(new QueryWrapper<Comment>().lambda().eq(Comment::getParentId, id));
        if ( comments.size() != 0 ){
            commentMapper.deleteBatchIds(comments);
        }
        commentMapper.delete(wrapper);
    }


}
